//
//  DownImageOperation.m
//  Operation
//
//  Created by tuyuer tuyuer on 9/6/11.
//  Copyright 2011 tuyuer. All rights reserved.
//

#import "HCDownImageOperation.h"

@interface UIImage(UIImageScale)
-(UIImage*)getSubImage:(CGRect)rect;
@end

@implementation UIImage(UIImageScale)

//截取部分图像
-(UIImage*)getSubImage:(CGRect)rect
{
    CGImageRef subImageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
    CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
    
    UIGraphicsBeginImageContext(smallBounds.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextDrawImage(context, smallBounds, subImageRef);
    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
    UIGraphicsEndImageContext();
    
    return smallImage;
}

@end


@implementation HCDownImageOperation
@synthesize sequenceNumber   = _sequenceNumber;

- (id)initWithUrl:(NSString *)imageUrl imageView:(UIImageView *)imageView
{
	if (self=[super init]) {
		m_imageUrl=[imageUrl copy];
		m_imageView=[imageView retain];
	}
	@synchronized([HCDownImageOperation class]){
		static NSUInteger sSequenceNumber;
		self->_sequenceNumber = sSequenceNumber;
		sSequenceNumber += 1;
	}
	return self;
}

- (id)initWithUrl:(NSString *)imageUrl imageView:(UIImageView *)imageView imageId:(NSString *)Id isRect:(BOOL)isrect
{
	if (self=[super init]) {
		m_imageUrl=[imageUrl copy];
		m_imageView=[imageView retain];
        m_imageId = [Id copy];
        rect = isrect;
	}
	@synchronized([HCDownImageOperation class]){
		static NSUInteger sSequenceNumber;
		self->_sequenceNumber = sSequenceNumber;
		sSequenceNumber += 1;
	}
	return self;
}



- (void)main
{
	[NSThread sleepForTimeInterval:.1f];
	NSData *imageData=[NSData dataWithContentsOfURL:[NSURL URLWithString:m_imageUrl]];
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *cachesDir = [paths objectAtIndex:0];
    NSString *imageName = [NSString stringWithFormat:@"%@",m_imageId];
    NSString *imageDocu = [cachesDir stringByAppendingPathComponent:imageName];

    if(imageData.length != 0)
    {
        UIImage *img=[UIImage imageWithData:imageData];
        
        NSLog(@"%@,%f %f",m_imageId,img.size.width,img.size.height);
        
        if(rect)
        {
            float bili;
            if(img.size.width>320)
                bili = img.size.width/320;
            else
                bili = 1;
            
            int offY;
            if(160*bili<160)
                offY = 0;
            else
                offY = (160*bili - 160)/2;
            
            NSLog(@"%f",(160*bili - 160)/2);
            
            img = [img getSubImage:CGRectMake(0, offY, img.size.width, 160*bili)];
            
            NSLog(@"%@,offy==%d,%f %f",m_imageId,offY,img.size.width,160*bili);
            
//            img = [img getSubImage:CGRectMake((img.size.width-320)/2, (img.size.height-320)/2, 320, 160)];//直接截取
        }
        NSData *data = UIImageJPEGRepresentation(img, 0.8);
        [[NSFileManager defaultManager] createFileAtPath:imageDocu contents:data attributes:nil];

        [m_imageView performSelectorOnMainThread:@selector(setImage:) withObject:img waitUntilDone:YES];
        
//        float bili;
//        if(img.size.height>160)
//            bili = img.size.width/160;
//        else
//            bili = 1;
//        m_imageView.frame = CGRectMake((320-img.size.width/bili)/2, m_imageView.frame.origin.x, img.size.width/bili, 160);
    }
}

- (void)dealloc{
    if(m_imageView)
    {
        [m_imageView release];
    }
    if(m_imageUrl)
    {
        [m_imageUrl release];
    }
	[super dealloc];
}

@end
